package com.suning.sawp.web.controller;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.suning.sawp.dto.oga.bi.DevelopSaleDetail;

/**
 * 总计平均展示逻辑, <br/>
 * 由服务器迭代集合处理页面展示逻辑是很傻逼的行为，但是接口已经定了没办法啊...
 * 
 *
 */
public class DevelopSaleDetailListDecorator<T extends DevelopSaleDetail> extends TotalAvgListDecoraor<T> {

    private List<T> developSaleDetailList;

    @SuppressWarnings("unchecked")
    public DevelopSaleDetailListDecorator(List<T> developSaleDetailList, T total, T avg) {
        this.developSaleDetailList = developSaleDetailList;
        if (CollectionUtils.isNotEmpty(developSaleDetailList)) {
            Integer[] develop = totalavg(
                    Iterables.toArray(Lists.transform(developSaleDetailList, new Function<T, Integer>() {

                        @Override
                        public Integer apply(DevelopSaleDetail input) {
                            return input.getDevelop();
                        }
                    }), Integer.class));
            Integer[] developed = totalavg(Iterables
                    .toArray(Lists.transform(developSaleDetailList, new Function<DevelopSaleDetail, Integer>() {

                        @Override
                        public Integer apply(DevelopSaleDetail input) {
                            return input.getDeveloped();
                        }
                    }), Integer.class));
            Integer[] developedOrder = totalavg(Iterables
                    .toArray(Lists.transform(developSaleDetailList, new Function<DevelopSaleDetail, Integer>() {

                        @Override
                        public Integer apply(DevelopSaleDetail input) {
                            return input.getDevelopedOrder();
                        }
                    }), Integer.class));
            BigDecimal[] saleAmount = totalavg(Iterables
                    .toArray(Lists.transform(developSaleDetailList, new Function<DevelopSaleDetail, BigDecimal>() {

                        @Override
                        public BigDecimal apply(DevelopSaleDetail input) {
                            return input.getSaleAmount();
                        }
                    }), BigDecimal.class));
            total.setDevelop(develop[0]);
            avg.setDevelop(develop[1]);
            total.setDeveloped(developed[0]);
            avg.setDeveloped(developed[1]);
            total.setDevelopedOrder(developedOrder[0]);
            avg.setDevelopedOrder(developedOrder[1]);
            total.setSaleAmount(saleAmount[0]);
            avg.setSaleAmount(saleAmount[1]);
            this.developSaleDetailList = Lists.newArrayList(developSaleDetailList);
            this.developSaleDetailList.addAll(0, Lists.<T> newArrayList(total, avg));
        }
    }

    @Override
    protected List<T> delegate() {
        return developSaleDetailList;
    }

}
